package com.mcf.xiaolinju.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcf.xiaolinju.vo.villageReturn;
import com.mcf.xiaolinju.entity.Village;
import com.mcf.xiaolinju.mapper.VillageMapper;
import com.mcf.xiaolinju.service.IVillageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2021-10-14
 */
@Service
public class VillageServiceImpl extends ServiceImpl<VillageMapper, Village> implements IVillageService {
    @Resource
    VillageMapper villageMapper;
    //通过小区的id查询出小区的信息
    public Village getById(Integer id){
        return  villageMapper.selectById(id);
    }
    //通过用户的id查出小区的信息
    public Village getByUserId(Integer id){
        return villageMapper.getByUserId(id);
    }
    //小区的删除
    public String del(int id, int updateid) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(villageMapper.del(id,updateid,ldt)>0){
            return "删除成功";
        }
        return "删除失败";
    }
   //小区修改
    public String update(Integer province, Integer city, Integer area, String adress, String villagename,  Integer updateid,Integer id) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(villageMapper.update(province,city,area,adress,villagename,updateid,id,ldt )>0){
            return "修改成功";
        }
        return "修改失败";
    }

    //分页查询
    public Page<villageReturn> selectpage(IPage<villageReturn> page) {
        return  villageMapper.selectpage(page);
    }
    //批量删除
    @Transactional(rollbackFor =  Exception.class) //回滚
    public String manydel(int[] id, int updateid) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(villageMapper.delmany(id,updateid,ldt)!=id.length){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败";
        }
        else{
            return "删除成功";
        }
    }

    //模糊查询
    public Page<villageReturn> selectdim(IPage<villageReturn> page, Integer provinceid, Integer cityid, Integer townid, String villagename) {
        return villageMapper.selectdim(page,provinceid,cityid,townid,villagename);
    }
   //小程序查询小区的id
    public int getvillageid(String commityname, String region) {
        String [] Provinces=region.split("-");
        int provinceid = Integer.parseInt(Provinces[0]);
        int cityid = Integer.parseInt(Provinces[1]);
        int regionid = Integer.parseInt(Provinces[2]);
        Integer num=villageMapper.getvillageid(commityname,provinceid,cityid,regionid);

        if(num!=null){
            return num;
        }
        else {
            return 0;
        }

    }
}

